# -*- coding: utf-8 -*-
# ----------------------------
# @Time    : 2022/6/18 2:29 PM
# @Author  : changqingai
# @FileName: broadcast_tile.py
# ----------------------------

import tensorflow as tf

# ********* broadcast **********
a = tf.ones([3, 4])
b = tf.broadcast_to(a, [2, 3, 4])
print("b:", b)

# ***********  自动broadcast_to(自动调整shape) *************
tensor1 = tf.random.uniform([2, 3], maxval=100, dtype=tf.int32, seed=1)
print("tensor1:", tensor1.numpy())

# 优先小维度对齐
tensor2 = tf.random.uniform([3], minval=0, maxval=10, dtype=tf.int32, seed=0)
print("tensor:", tensor2.numpy())
tensor = tensor2 + tensor1
print(tensor.numpy())

# ***********  使用tf.tile手动调整shape，然后进行运行 *************
print("***********  使用tf.tile手动调整shape，然后进行运行 *************")
tensor1 = tf.random.uniform([2, 4], maxval=10, dtype=tf.int32, seed=1)
print("tensor1: ", tensor1.numpy())
tensor2 = tf.random.uniform([2, ], maxval=10, dtype=tf.int32, seed=2)
print("tile前 tensor2:", tensor2.numpy())

tensor2 = tf.expand_dims(tensor2, axis=0)
tensor2 = tf.tile(tensor2, [2, 2])
print("tile后 tensor2:", tensor2.numpy())

tensor = tensor2 + tensor1
print("tensor: ", tensor.numpy())


# ***********  直接使用tf.broadcast_to(等同于tf.expand_dim + tf.tile *************
print("***********  使用tf.tile手动调整shape，然后进行运行 *************")
tensor1 = tf.random.uniform([2, 4], maxval=10, dtype=tf.int32, seed=1)
print("tensor1: ", tensor1.numpy())
tensor2 = tf.random.uniform([2, 1], maxval=10, dtype=tf.int32, seed=2)
print("tile前 tensor2:", tensor2.numpy())

tensor2 = tf.broadcast_to(tensor2, [2, 4])
tensor = tensor2 + tensor1
print("tensor: ", tensor.numpy())

tf.norm